package task1205.Demo.Demo3;

import task1205.Demo.Demo2.Demo1;
import task1205.Demo.Demo4.NetService;

public class NetPackage extends SercicePackage implements NetService {
    private int flow;
    public NetPackage(){
        this.flow = 1024*3;
        this.price = 68.0;
    }

    public int getFlow() {
        return flow;
    }

    public void setFlow(int flow) {
        this.flow = flow;
    }

    @Override
    public void ShowInfo() {
        System.out.println("网虫套餐：上网流量是"+flow/1024+"GB/月，月资费是"+this.price+"元/月");
    }

    public void netPlay2(int flow, Demo2 demo2) throws Exception {
        int reminFlow = this.flow = demo2.getRealFlow();
        if (this.flow <= reminFlow){
            demo2.setRealFlow(demo2.getRealFlow() + flow);
        }else {
            double consumeMoney = 0.1 * (flow-reminFlow);
            if (demo2.getMoney() >= consumeMoney){
                demo2.setRealFlow(demo2.getRealFlow() + flow);
                demo2.setMoney(demo2.getMoney() - consumeMoney);
                demo2.setConsumAmount(demo2.getConsumAmount() + consumeMoney);
            }else {
                int temp = (int)(demo2.getMoney()/0.1);
                throw new Exception("您的余额不足，请充值后在使用");
            }
        }
    }

    @Override
    public int netPlay(int count, Demo2 demo2) throws Exception {
        int temp = flow;
        for (int i = 0; i < flow; i++) {
            if (this.flow - demo2.getRealFlow() >= 1){
                demo2.setRealFlow(demo2.getRealFlow() + 1);
            }else if (demo2.getMoney()>=0.1){
                demo2.setRealFlow(demo2.getRealFlow()+1);
                demo2.setMoney(Demo1.sub(demo2.getMoney(),0.1));
                demo2.setConsumAmount(demo2.getConsumAmount() + 0.1);
            }else {
                temp = i;
                throw new Exception("本次已使用流量"+i+"MB,您的余额不足，请充值后在使用");
            }
        }
        return temp;
    }
}
